package 代码记录.剑指offer错题本;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
        generateMatrix(3);
    }
    public static int[][] generateMatrix(int n) {
        int left = 0,right = n - 1,up = 0,down = n - 1;
        int count = 0;
        int[][] res = new int[n][n];
        while(count < n * n){
            for(int i = left;i <= right && count < n * n;i++){
                res[up][i] = count + 1;
                count++;
            }
            up++;
            for(int i = up;i <= down && count < n * n;i++){
                res[i][right] = count + 1;
                count++;
            }
            right--;
            for(int i = right;i >= left && count < n * n;i--){
                res[down][i] = count + 1;
                count++;
            }
            down--;
            for(int i = down;i >= up && count < n * n;i--){
                res[i][left] = count + 1;
                count++;
            }
            down++;
        }
        return res;
    }
}